#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000+10;
int N, M;

struct E{
    int b, c;
    E(int b, int c):b(b), c(c){}
    bool operator < (const E &t){
        return this->c < t.c;
    }
};
bool vis[maxn];
int d[maxn];
vector<E> e[maxn];

void bfs1(){
    queue<int> q;
    q.push(N); vis[N] = true; d[N] = 0;
    while(!q.empty()){
        int v = q.front(); q.pop();
        if(v == 1) return;
        for (int i = 0; i < e[v].size(); ++i) {
            int nex = e[v][i].b;
            if(!vis[nex]){
                vis[nex] = true;
                d[nex] = d[v]+1;
                q.push(nex);
            }
        }
    }
}

vector<int> ans;

void bfs2(){
    ans.clear(); memset(vis, false, sizeof(vis));
    vector<int> a;
    a.push_back(1); vis[1] = true;
    while(true) {
        int mini = INT_MAX;
        for (int i = 0; i < a.size(); ++i) {
            int u = a[i];
            for (int j = 0; j < e[u].size(); ++j) {
                int v = e[u][j].b;
                if (d[u] == d[v] + 1) mini = min(mini, e[u][j].c);
            }
        }
        ans.push_back(mini);
        vector<int> a2;
        for (int i = 0; i < a.size(); ++i) {
            int u = a[i];
            for (int j = 0; j < e[u].size(); ++j) {
                int v = e[u][j].b; if(v==N) return;
                if (d[u] == d[v] + 1 && e[u][j].c == mini && !vis[v]){
                    a2.push_back(v);
                    vis[v] = true;
                }
            }
        }
        a = a2;
    }
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    while(cin >> N >> M){
        memset(vis, false, sizeof(vis));
        memset(d, 0, sizeof(d));
        for (int i = 0; i < maxn; ++i) {
            e[i].clear();
        }
        for (int i = 0; i < M; ++i) {
            int a, b, c;
            cin >> a >> b >> c;
            e[a].emplace_back(b, c);
            e[b].emplace_back(a, c);
        }

        bfs1();
        cout << d[1] << "\n";
        bfs2();
        for (int i = 0; i < ans.size(); ++i) {
            if(i) cout << " ";
            cout << ans[i];
        }
        cout << "\n";
    }
    return 0;
}